package 剑指offer;

import java.util.LinkedList;
import java.util.List;

/**
 * @description:
 * @author: ywk
 * @date: 2021-03-13
 */
public class 烧饼排序 {
    static LinkedList<Integer> res = new LinkedList<>();

    static List pancakeSort(int[] cakes) {
        sort(cakes, cakes.length);
        return res;
    }

    static void sort(int[] cakes, int n) {
        if (n == 1) {
            return;
        }
        int maxCake = 0;
        int maxCakeIndex = 0;
        for (int i = 0; i < n; i++) {
            if (cakes[i] > maxCake) {
                maxCakeIndex = i;
                maxCake = cakes[i];
            }
        }
        //第一次翻转，将最大饼翻到最上面
        reverse(cakes, 0, maxCakeIndex);
        res.add(maxCakeIndex + 1);
        reverse(cakes, 0, n - 1);
        res.add(n);

        sort(cakes, n - 1);

    }

    static private void reverse(int[] arr, int i, int j) {
        while (i < j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            i++;
            j--;
        }
    }

    public static void main(String[] args) {
        pancakeSort(new int[]{3, 2, 4, 1});
        System.out.println(res);
    }
}
